import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_tv_widgets/widgets/tabView/kq_tab_view.dart';
import '../../config/kq_global.dart';
import '../../resources/images.dart';
import '../../resources/kq_theme_colors.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import '../../resources/l10n/kq_intl.dart';
import '../../theme/kq_theme_manager.dart';

/// 自定义弹窗
class KqCustomDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  final Widget customWidget;

  /// 按钮的自定义布局
  final Widget? customBtnWidget;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 左边按钮回调
  final Function()? onLeftBtnTap;

  /// 右边按钮回调
  final Function()? onRightBtnTap;

  /// 标题文字颜色
  final Color? titleColor;

  /// 标题文字大小
  final double? titleFontSize;

  //只显示左侧
  final bool isShowLeft;

  //只显示右侧
  final bool isShowRight;

  //屏幕宽度
  final double? width;

  /// 是否显示标题右侧的关闭按钮，默认false
  final bool showClose;

  /// 标题右侧关闭按钮点击回调
  // final VoidCallback? onClose;

  const KqCustomDialog({
    Key? key,
    this.titleString,
    required this.customWidget,
    this.customBtnWidget,
    this.leftBtnText,
    this.rightBtnText,
    this.titleColor,
    this.titleFontSize,
    this.onLeftBtnTap,
    this.onRightBtnTap,
    this.isShowLeft = true,
    this.isShowRight = true,
    this.width,
    this.showClose = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final List<Widget> children = <Widget>[];

    /// 标题
    if (titleString != null) {
      children.add(Container(
        padding: EdgeInsets.only(left: 32.r, right: showClose ? 4.r : 32.r),
        child: Row(
          children: [
            Expanded(
                child: Container(
              padding: EdgeInsets.only(top: 24.r, bottom: 16.r),
              alignment: Alignment.center,
              child: Text(
                titleString!,
                style: TextStyle(
                  fontSize: 28.sp,
                  color: KqThemeManager.instance
                          .getConfig()
                          .dialogConfig
                          .titleColor ??
                      KqThemeColors.textBlack85,
                ),
              ),
            )),
            if (showClose)
              KqInkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                verticalPadding: 15.5.r,
                horizontalPadding: 20.r,
                radius: 4.r,
                child: Image.asset(
                  Images.commonIcGuanbiDialog,
                  width: 48.r,
                  height: 48.r,
                  fit: BoxFit.fill,
                  package: KqGlobal.packageName,
                ),
              )
          ],
        ),
      ));
      // children.add(Container(
      //   padding: EdgeInsets.symmetric(horizontal: 32.r),
      //   alignment: Alignment.center,
      //   child: Text(
      //     titleString!,
      //     style: TextStyle(
      //       fontSize: 28.sp,
      //       color:
      //           KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
      //               KqThemeColors.textBlack85,
      //     ),
      //   ),
      // ));
      // children.add(SizedBox(
      //   width: 1.r,
      //   height: 16.r,
      // ));
    } else {
      /// 上边距
      children.add(SizedBox(
        width: 1.r,
        height: 24.r,
      ));
    }

    //自定义内容
    children.add(customWidget);

    /// 下边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    if (customBtnWidget != null) {
      children.add(customBtnWidget!);
    } else {
      if (isShowLeft && !isShowRight) {
        //显示左侧 右侧不显示
        children.add(Container(
            padding: EdgeInsets.symmetric(
              horizontal: KqThemeManager.instance
                      .getConfig()
                      .formConfig
                      .paddingHorizontal ??
                  24.r,
              vertical: KqThemeManager.instance
                      .getConfig()
                      .formConfig
                      .paddingVertical ??
                  16.r,
            ),
            child: KqInkWell(
              onTap: (() {
                if (onLeftBtnTap != null) {
                  onLeftBtnTap!();
                }
              }),
              radius: 2.r,
              borderWidth: 1.5.r,
              borderColor: KqThemeColors.bgBlue,
              backgroundColor: KqThemeColors.bgDialog,
              child: Container(
                height: 72.r,
                alignment: Alignment.center,
                child: Text(
                  leftBtnText ?? KqIntl.currentResource.cancel,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style:
                      TextStyle(fontSize: 28.sp, color: KqThemeColors.textBlue),
                ),
              ),
            )));
      } else if (isShowRight && !isShowLeft) {
        children.add(Container(
            padding: EdgeInsets.symmetric(
              horizontal: KqThemeManager.instance
                      .getConfig()
                      .formConfig
                      .paddingHorizontal ??
                  24.r,
              vertical: KqThemeManager.instance
                      .getConfig()
                      .formConfig
                      .paddingVertical ??
                  16.r,
            ),
            child: KqInkWell(
              onTap: (() {
                if (onRightBtnTap != null) {
                  onRightBtnTap!();
                }
              }),
              radius: 2.r,
              borderWidth: 1.5.r,
              borderColor: KqThemeColors.bgBlue,
              backgroundColor: KqThemeColors.bgBlue,
              child: Container(
                height: 72.r,
                alignment: Alignment.center,
                child: Text(
                  rightBtnText ?? KqIntl.currentResource.confirm,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                      fontSize: 28.sp, color: KqThemeColors.textWhite),
                ),
              ),
            )));
      } else if (isShowLeft && isShowRight) {
        children.add(KqTabView(
          titles: [
            leftBtnText ?? KqIntl.currentResource.cancel,
            rightBtnText ?? KqIntl.currentResource.confirm
          ],
          colorTypes: const [ColorType.lineBlue, ColorType.fillBlue],
          tabIndexBlock: (index, disable) {
            if (index == 0) {
              if (onLeftBtnTap != null) {
                onLeftBtnTap!();
              }
            } else if (index == 1) {
              if (onRightBtnTap != null) {
                onRightBtnTap!();
              }
            }
          },
        ));
      }
    }

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    return UnconstrainedBox(
        child: SizedBox(
            width: width ?? 880.r,
            child: Material(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(
                        KqThemeManager.instance
                                .getConfig()
                                .dialogConfig
                                .radius ??
                            4.r))),
                color:
                    KqThemeManager.instance.getConfig().dialogConfig.bgColor ??
                        KqThemeColors.bgWhite,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          4.r)),
                  child: dialogChild,
                ))));
  }
}
